﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Hzexe.Wechat.SharpSDK;
using Hzexe.Wechat.SharpSDK.Message.Response;

namespace WebApp
{
    /// <summary>
    /// 微信请求路由
    /// </summary>
    public class wechat : WechatBase
    {
        /// <summary>
        /// 您的微信token
        /// </summary>
        const string _token = "ssss";

        /// <summary>
        /// 获取token的方法
        /// </summary>
        protected override string token
        {
            get { return _token; }
        }
        /// <summary>
        /// 收到文本消息的处理
        /// </summary>
        /// <param name="reqPackage">文本消息</param>
        /// <returns>应答消息</returns>
        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Text reqPackage)
        {
            /*
             * 函数的返回值是基类型，况且微信也并不要求请求消息是文本的就要回复文本的，
             * 回复几种消息类型都可以 Hzexe.Wechat.SharpSDK.Message.Response，命名空间下以Message开头的都是可用的消息类型
             * 这里的例子是收到文本类型的消息，当作歌名，把检索到的结果取第一个，作为音乐类型发送到用户
             * 
             ***/

            Message_Out_Music resp = new Message_Out_Music(reqPackage);
            resp.Music = new Music_item()
            {
                Title = "蓝莲花--打字不如听歌，你说呢",
                MusicUrl = "http://stream16.qqmusic.qq.com/30169202.mp3",
                Description = reqPackage.Content,
                HQMusicUrl = "http://stream16.qqmusic.qq.com/30169202.mp3"
            };
            return resp;
        }
        /// <summary>
        /// 收到图片消息消息的处理
        /// </summary>
        /// <param name="reqPackage">图片消息消息</param>
        /// <returns>应答消息</returns>
        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Image reqPackage)
        {
            //回文本吧
            Message_Out_Text resp = new Message_Out_Text(reqPackage);
            resp.Content = string.Format("你发的图片已保存到服务器:{0}", reqPackage.PicUrl);
            return resp;
        }
        /// <summary>
        /// 收到地理位置消息的处理
        /// </summary>
        /// <param name="reqPackage">地理位置消息</param>
        /// <returns>应答消息</returns>
        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Location reqPackage)
        {
            //回文本吧
            Message_Out_Text resp = new Message_Out_Text(reqPackage);
            resp.Content = string.Format("你在:{0}", reqPackage.Label);
            return resp;
        }
        /// <summary>
        /// 收到链接消息的处理
        /// </summary>
        /// <param name="reqPackage">链接消息</param>
        /// <returns>应答消息</returns>
        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Link reqPackage)
        {
            //回文本吧
            Message_Out_Text resp = new Message_Out_Text(reqPackage);
            resp.Content = string.Format("你发送了:{0}", reqPackage.Url);
            return resp;
        }
        
        // <summary>
        /// 收到未能识别的消息的处理
        /// </summary>
        /// <param name="reqPackage">新类型的消息</param>
        /// <returns>应答消息</returns>
        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Unknown reqPackage)
        {
            //这里要记录下，说明微信的数据包升级了，需要到https://wechatsharp.codeplex.com，获取新的版本
            //直接回复回去
            Message_Out_Text resp = new Message_Out_Text(reqPackage);
            resp.Content = string.Format("未能识别的消息,请更新wechatsharp库:\r\n{0}", reqPackage.originalxmlstr);
            return resp;
        }
        /// <summary>
        /// 新用户关注
        /// </summary>
        /// <param name="reqPackage"></param>
        /// <returns></returns>
        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Event_Subscribe reqPackage)
        {
            Message_Out_Text resp = new Message_Out_Text(reqPackage);
            resp.Content = string.Format("感谢关注！", reqPackage.originalxmlstr);
            return resp;
        }
        /// <summary>
        /// 取消关注
        /// </summary>
        /// <param name="reqPackage"></param>
        /// <returns></returns>
        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Event_unsubscribe reqPackage)
        {
            //取消关注，回复的消息用户已收不到，但可以做一些操作，比如从数据库中标记或移除用户的信息
            Message_Out_Text resp = new Message_Out_Text(reqPackage);
            resp.Content = string.Format("bye bye！", reqPackage.originalxmlstr);
            return resp;
        }
        /// <summary>
        /// 用户点击菜单上按钮
        /// </summary>
        /// <param name="reqPackage"></param>
        /// <returns></returns>
        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Event_MenuClicked reqPackage)
        {
            //用户点击菜单上按钮的事件
            Message_Out_Text resp = new Message_Out_Text(reqPackage);
            resp.Content = string.Format("您点击了按钮标识：{0}", reqPackage.getMenuID);
            return resp;
        }

        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Voice reqPackage)
        {
            Message_Out_News resp = new Message_Out_News(reqPackage);
            resp.Articles.Add(new Article_Item()
            {
                Description = "一个一个的呀",
                PicUrl = "http://res.wx.qq.com/mpres/htmledition/images/login/logo.png",
                Title="test",
                Url="https://wechatsharp.codeplex.com"
            }
            );
            resp.Articles.Add(new Article_Item()
            {
                Description = "二个二个的呀呀",
                PicUrl = "http://img.wdjimg.com/image/www/p3.jpg",
                Title = "test",
                Url = "https://wechatsharp.codeplex.com"
            }
            );
            return resp;
        }

        protected override ResponseMessageBase OnReceiveNewMsg(Hzexe.Wechat.SharpSDK.Message.Request.Message_In_Video reqPackage)
        {
            Message_Out_Text resp = new Message_Out_Text(reqPackage);
            resp.Content = string.Format("我看不懂视频：{0}", reqPackage.MediaId);
            return resp;
        }
    }
}